// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.tensor.functions;

import com.google.common.annotations.Beta;
import com.yahoo.tensor.DimensionSizes;
import com.yahoo.tensor.IndexedTensor;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.EvaluationContext;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

/**
 * An indexed tensor whose values are generated by a function
 * 
 * @author bratseth
 */
@Beta
public class Generate extends PrimitiveTensorFunction {

    private final TensorType type;
    private final Function<List<Integer>, Double> generator;

    /**
     * Creates a generated tensor
     * 
     * @param type the type of the tensor
     * @param generator the function generating values from a list of ints specifying the indexes of the
     *                  tensor cell which will receive the value
     * @throws IllegalArgumentException if any of the tensor dimensions are not indexed bound
     */
    public Generate(TensorType type, Function<List<Integer>, Double> generator) {
        Objects.requireNonNull(type, "The argument tensor type cannot be null");
        Objects.requireNonNull(generator, "The argument function cannot be null");
        validateType(type);
        this.type = type;
        this.generator = generator;
    }
    
    private void validateType(TensorType type) {
        for (TensorType.Dimension dimension : type.dimensions())
            if (dimension.type() != TensorType.Dimension.Type.indexedBound)
                throw new IllegalArgumentException("A generated tensor can only have indexed bound dimensions");
    }

    @Override
    public List<TensorFunction> functionArguments() { return Collections.emptyList(); }

    @Override
    public TensorFunction replaceArguments(List<TensorFunction> arguments) {
        if ( arguments.size() != 0)
            throw new IllegalArgumentException("Generate must have 0 arguments, got " + arguments.size());
        return this;
    }

    @Override
    public PrimitiveTensorFunction toPrimitive() { return this; }
    
    @Override
    public Tensor evaluate(EvaluationContext context) {
        Tensor.Builder builder = Tensor.Builder.of(type);
        IndexedTensor.Indexes indexes = IndexedTensor.Indexes.of(dimensionSizes(type));
        for (int i = 0; i < indexes.size(); i++) {
            indexes.next();
            builder.cell(generator.apply(indexes.toList()), indexes.indexesForReading());
        }
        return builder.build();
    }
    
    private DimensionSizes dimensionSizes(TensorType type) {
        DimensionSizes.Builder b = new DimensionSizes.Builder(type.dimensions().size());
        for (int i = 0; i < b.dimensions(); i++)
            b.set(i, type.dimensions().get(i).size().get());
        return b.build();
    }
    
    @Override
    public String toString(ToStringContext context) { return type + "(" + generator + ")"; }

}
